package com.itheima.aop;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 演示AOP中的通知类型
 */
@Slf4j
//@Aspect
//@Component
public class AspectDemo1 {

    //抽取切入点表达式
    @Pointcut("execution(public * com.itheima.service.*.*(..))")
    public void pointcut() {
    }

    //环绕通知
    @SneakyThrows
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) {
        //环绕前（前置通知）
        log.info("环绕通知-前");
        Object res = pjp.proceed();
        //环绕后（后置通知）
        log.info("环绕通知-后");
        return res;
    }

    //前置通知
    @Before("pointcut()")
    public void before(JoinPoint jp) {
        log.info("前置通知");
    }

    //后置通知（最终通知）
    @After("pointcut()")
    public void after(JoinPoint jp) {
        log.info("最终通知");
    }

    //后置通知
    @AfterReturning("pointcut()")
    public void afterReturning(JoinPoint jp) {
        log.info("后置通知");
    }

    //异常通知
    @AfterThrowing("execution(public * com.itheima.service.*.*(..))")
    public void afterThrowing(JoinPoint jp) {
        log.info("异常通知");
    }


}
